<template>
  <div>

     <myheader></myheader>

		
		<div id="carousel" class="carousel slide" data-ride="carousel">
		
	

	
			<div class="carousel-inner">
			
				<!--Text only with background image-->
				<div class="carousel-item active">

					<!-- 轮播图 -->

					<Carousel @change="changeimg" @click="clickimg" pageTheme='circle' :datas='imgs'></Carousel>

				</div>
				
				
			
			</div>
		</div>
		<section class="collections text-center ">
			<div class="container-fluid">
				<div class="row">
					<div class="collection col-md-6 alt-background">
						<div class="container container-right text-center">
							<div>
								<h1>Women's</h1>
								<p class="lead">Spring/Summer 2018 Collection</p>
								<a href="catalog.html" class="btn btn-outline-secondary">Browse Women's</a>
							</div>
						</div>
					</div>
					<div class="collection col-md-6 bg-secondary inverted">
						<div class="container container-left text-center">
							<div>
								<h1>Men's</h1>
								<p class="lead">Spring/Summer 2018 Collection</p>
								<a href="catalog.html" class="btn btn-outline-white">Browse Men's</a>
							</div>
						</div>
					</div>
				</div>
			</div>
    </section>
    
		<section class="featured-block text-center">
			<div class="container">
				<div class="row justify-center">
					<div class="col-md-6 text-center">
						<img class="mt-4 mb-4 img-fluid" src="../assets/images/placeholder-jacket.png" style="width: 100%;">
					</div>
					<div class="col-md-6 text-center text-md-left">
						<h2 class="mb-3">Spring/Summer Collection 2018</h2>
						<p class="lead mt-2 mb-3">Lorem ipsum dolor sit amet, consectetur adipiscing elit. Suspendisse cursus erat sed sem sagittis cursus.</p>
						<p>Lorem ipsum dolor sit amet, consectetur adipiscing elit. Donec sed odio dui. Suspendisse cursus erat sed sem sagittis cursus. Etiam porta sem malesuada magna mollis euismod.</p>
						<a href="#" class="btn btn-md btn-outline-primary mt-3">Shop Now</a>
					</div>
				</div>
			</div>
		</section>
		
		<section class="products text-center">
			<div class="container">
				<h3 class="mb-4">商品列表</h3>
				<div class="row">
					<div v-for='item in goodslist' class="col-sm-6 col-md-3 col-product">
						<figure>
							<img class="rounded-corners img-fluid" :src="'http://127.0.0.1:8000/static/upload/'+item.img"	width="240" height="240">
							<figcaption>
								<div class="thumb-overlay"><a href="item.html" title="More Info">
									<i class="fas fa-search-plus"></i>
								</a></div>
							</figcaption>
						</figure>
						<h4><a :href="'http://127.0.0.1:8080/item?id='+item.id">{{item.name}}</a></h4>
						<p><span class="emphasis">${{item.price}}</span></p>
					</div>

				</div>

				<br>

				<!--heyui分页-->
				<div>

					<!-- <Pagination v-model="pagination" @change="get_goods" layout="pager,jumper" small ></Pagination> -->


					<Pagination v-model="pagination" @change="get_goods" ></Pagination>
					
				</div>



				<!-- 自定义分页 -->
				<div class="row">
					
					<div v-for="item in goodslist_self" class="col-sm-6 col-md-3 col-product">
						<figure>
							<img :src="'http://localhost:8000/static/upload/'+item.img" class="rounded-corners img-fluid" 	width="240" height="240">
							<figcaption>
								<div class="thumb-overlay"><a href="item.html" title="More Info">
									<i class="fas fa-search-plus"></i>
								</a></div>
							</figcaption>
						</figure>
						<h4><a :href="'http://localhost:8080/item?id='+item.id">{{ item.name }}</a></h4>
						<p><span class="emphasis">${{ item.price }}</span></p>
					</div>

				</div>

				<!--自主分页容器-->
				<div>

				<a @click="get_goods_self(1)"  >首页</a>


				<Button v-show="lastpage" @click="get_goods_self(lastpage)">上一页</Button>

				&emsp;&emsp;

				<!-- <span v-for="index in allpage">
					
					<a @click="get_goods_self(index)"  >{{ index }}</a>&emsp;&emsp;

				</span> -->

				&emsp;&emsp;

				<span v-for="item in last_page">
					
					<a @click="get_goods_self(item)"  >{{ item }}</a>&emsp;&emsp;

				</span>

				<a @click="get_goods_self(page)"  >{{ page }}</a>&emsp;&emsp;


				<span v-for="item in next_page">
					
					<a @click="get_goods_self(item)"  >{{ item }}</a>&emsp;&emsp;

				</span>

				<span v-if='page != allpage'>...</span>

				<Button v-show="nextpage" @click="get_goods_self(nextpage)">下一页</Button>


				<a @click="get_goods_self(allpage)"  >尾页</a>
				
				<input @input="jump_page($event)" type="text" style="width:50px;">
				</div>

				<div>

					<Button @click="get_goods_self(page,'id','')">id正序</Button>
					<Button @click="get_goods_self(page,'id','-')">id倒序</Button>
					<Button @click="get_goods_self(page,'price','')">价格正序</Button>
					<Button @click="get_goods_self(page,'price','-')">价格倒序</Button>

				</div>



			</div>
		</section>
		
		<div class="divider"></div>
		
		<section class="cta text-center">
			<div class="container">
				<h3 class="mt-0 mb-4">Sign up now to save 10% on your first order</h3>
				<form class="subscribe">
					<div class="form-group row pt-3">
						<div class="col-sm-8 mb-3">
							<input type="text" class="form-control" id="inputName" placeholder="Your Name">
						</div>
						<div class="col-sm-4">
							<button type="submit" class="btn btn-lg btn-outline-primary">Sign me up</button>
						</div>
					</div>
				</form>
			</div>
		</section>
		


		<myfooter></myfooter>
    
  </div>
  
</template>


 
<script>

import myheader from './myheader'
import myfooter from './myfooter'


export default {
  data () {
    return {
	  msg: "这是一个变量",
	  //轮播图图片
	  imgs:[
		  {
			  title:'广告活动页',
			  link:'http://www.baidu.com',
			  image:'http://localhost:8080/static/img/placeholder-jacket.f5996c6.png'
		  },
		  {
			  title:'促销活动页',
			  link:'http://www.163.com',
			  image:'http://localhost:8080/static/img/placeholder-product.1011f1b.jpg'
		  }
	  ],
	  goodslist:[],
	  goodslist_self:[],

      //分页器变量
      pagination:{
      	page:1,
      	size:2,
      	total:5
	  },
	  
	  //自定义分页
      //商品总数
      total_self:0,
      //上一页
      lastpage:0,
      //下一页
      nextpage:0,
      //当前页
      page:1,
      //总页数
      allpage:0,
      //每页展示数
	  size:1,
	  
	  //分页偏移
      last_page:[],
	  next_page:[],
	  
	  //分页方案
	  coloum:'',
	  order:'',

    }
  },
  components:{
	  "myheader":myheader,
	  'myfooter':myfooter
  },
  mounted:function(){

	  this.get_carousel()
	  this.get_goods()

	  this.get_goods_self(1);
},
  methods:{


	  //页面跳转
	  jump_page(e){

		  var val = e.target.value;
		  if(val > this.allpage){
			  this.$Message('您输入的页码有误')
			  return false
		  }

		  this.get_goods_self(val)
		

	  },

	  //获取商品接口
	  get_goods(){
		  this.axios.get('http://127.0.0.1:8000/goodslist/',{params:{page:this.pagination.page,size:this.pagination.size}}).then(res=>{

			  console.log(res)
			  this.goodslist = res.data.data
			  this.pagination.total = res.data.total
			  
		  })
		  
	  },

	  
    //自主分页
    get_goods_self:function(page,coloum,order){

		this.page = page

		var coloum = coloum
		var order = order

		if(coloum||order){
			this.coloum = coloum
			this.order = order
		}

	
      //发送请求
      this.axios.get('http://localhost:8000/goodslist/',{params:{page:page,size:this.size,coloum:this.coloum,order:this.order}}).then((result) =>{

        console.log(result);

        this.goodslist_self = result.data.data;

        //商品总数
        this.total_self= result.data.total;

        //判断上一页
        if(page==1){
        	this.lastpage = 0;
        }else{
        	this.lastpage = page - 1;
        }

        //计算总页数
        this.allpage = Math.ceil(this.total_self / this.size);

        //判断下一页
        if(page == this.allpage){
        		this.nextpage = 0;
        }else{
        		this.nextpage = page + 1;
		}
		

        //设置偏移量
        var move_page = 2;

        var my_last = [];

        //计算左侧偏移量
        for(let i=page-move_page;i<page;i++){

        	if(i > 0){

        	my_last.push(i);

        	}
        }

        //计算右侧偏移量

        var my_next = [];

        for(let i=page+1;i<=page+move_page;i++){

        	if(i <= this.allpage){

        	my_next.push(i);

        	}

        }

        console.log(my_last);

        console.log(my_next);

        this.last_page = my_last;
        this.next_page = my_next;



      });

    },


	  //获取轮播图接口
	  get_carousel(){
		  this.axios.get('http://127.0.0.1:8000/getcarousel/').then(res=>{
			  
			  var mylist = [];
			  //遍历循坏
			  for (let i=0,l=res.data.length;i<l;i++){

				  
				  mylist.push({title:res.data[i].name,link:res.data[i].src,image:res.data[i].img})
			  }
			  console.log(mylist)
			  this.imgs = mylist
			  
		  })
		  
	  },


	  //点击轮播图
	  clickimg(index,data){

		//   alert(data.link)

		//跳转
		window.location.href = data.link
	  },

	  //切换轮播图
	  changeimg(index,data){

		  console.log(data)
	  },

	  
     
  }
}


</script>
 
<style>



</style>